<template>
  <div class="location">
    <img
      :src="hospitalImg"
      usemap="#bodyMap"
      alt=""
      style="cursor: crosshair"
      ref="hospitalRef"
      @mousedown="startDrawing"
    />
    <!--  <map name="bodyMap">
      <area
        v-for="(area, index) in areaParts"
        :key="index"
        shape="rect"
        :coords="area.coords"
        :alt="area.name"
      />
    </map> -->
  </div>
</template>

<script lang="ts" setup>
import L from "leaflet";
import "leaflet/dist/leaflet.css";
import hospitalImg from "@/assets/images/hospital.png";
import { onMounted, ref, onBeforeUnmount } from "vue";
let isDrawing = false;
let startX = 0;
let startY = 0;
const hospitalRef = ref<HTMLImageElement>();
onMounted(() => {
  const res = searchPath(0, 1, adjacencyMatrix);
  console.log(res, "res...");
});
const startDrawing = (e: MouseEvent) => {
  console.log("startDrawing");
  isDrawing = true;
  const rect = hospitalRef.value?.getBoundingClientRect();
  if (rect) {
    startX = e.clientX - rect.left;
    startY = e.clientY - rect.top;
    console.log(startX, startY, "down");
  }
};
/* const areaParts = [
  { name: "gudience", coords: "165, 245,208, 278 ", cnName: "导诊台" },

  { name: "hospita", coords: "103, 433 ,128 ,503", cnName: "医院" },
]; */
var points = {
  0: { name: "导诊台", position: [178, 258, 0] },
  1: { name: "医院1", position: [103, 448, 0] },
};
var routes = [
  { src: 0, des: 1, cost: 12.345, extra: "0-1" },
  { src: 1, des: 0, cost: 12.345, extra: "1-0" },
];
const numNodes = 2;

// 初始化邻接矩阵，所有元素设为 Infinity
const adjacencyMatrix = Array.from({ length: numNodes }, () =>
  Array(numNodes).fill(Infinity)
);

// 根据已知的连接信息填充邻接矩阵
// 例如，节点 0 到节点 1 的代价为 12.345
adjacencyMatrix[0][1] = 12.345;

// 节点 1 到节点 0 的代价也是 12.345（如果是无向图）
adjacencyMatrix[1][0] = 12.345;

// 继续填充其他连接...

// 打印邻接矩阵
console.log(adjacencyMatrix, "aa");
/**
 * @param {number} v 起点索引
 * @param {number} d 终点索引
 * @param {Array} adjMatrix 邻接矩阵
 */
function searchPath(v: number, d: number, adjacencyMatrix: number[][]) {
  const numNodes = adjacencyMatrix.length;

  const distances: number[] = Array(numNodes).fill(Infinity);

  const previous: (number | null)[] = Array(numNodes).fill(null);

  const visited: boolean[] = Array(numNodes).fill(false);

  distances[v] = 0;

  for (let i = 0; i < numNodes - 1; i++) {
    const u = findMinDistanceNode(distances, visited);

    if (u === null) break; // 如果没有可访问的节点，退出循环

    visited[u] = true;

    for (let neighbor = 0; neighbor < numNodes; neighbor++) {
      if (adjacencyMatrix[u][neighbor] !== Infinity && !visited[neighbor]) {
        const alt = distances[u] + adjacencyMatrix[u][neighbor];

        if (alt < distances[neighbor]) {
          distances[neighbor] = alt;

          previous[neighbor] = u;
        }
      }
    }
  }

  // 重建路径

  const path: number[] = [];

  let currentNode: number | null = d;

  while (currentNode !== null) {
    path.unshift(currentNode);

    currentNode = previous[currentNode];
  }

  // 如果路径不包含起点，说明不可达

  if (path[0] !== v) {
    return { distance: Infinity, path: [] };
  }

  return { distance: distances[d], path };
}
function findMinDistanceNode(
  distances: number[],
  visited: boolean[]
): number | null {
  let minDistance = Infinity;
  let minIndex = -1;

  for (let i = 0; i < distances.length; i++) {
    if (!visited[i] && distances[i] < minDistance) {
      minDistance = distances[i];
      minIndex = i;
    }
  }

  return minIndex === -1 ? null : minIndex;
}
</script>

<style lang="scss" scoped>
.location {
}
</style>
